# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=pygobject
pkgbase=mingw-w64-${_realname}2
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-gobject2" "${MINGW_PACKAGE_PREFIX}-python3-gobject2" "${MINGW_PACKAGE_PREFIX}-pygobject2-devel")
pkgver=2.28.6
pkgrel=6
pkgdesc="Allows you to load glade interface files in a program at runtime (mingw-w64)"
arch=(any)
url="http://www.pygtk.org"
license=('LGPL')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2" "${MINGW_PACKAGE_PREFIX}-python3" "${MINGW_PACKAGE_PREFIX}-gobject-introspection" "${MINGW_PACKAGE_PREFIX}-gnome-common")
options=('staticlibs' 'strip')
source=(https://download.gnome.org/sources/pygobject/${pkgver%.*}/${_realname}-${pkgver}.tar.xz
        win32-fix.patch
        python-m4.patch
        gio-types.patch)
sha256sums=('fb8a1d4f665130a125011659bd347c7339c944232163dbb9a34fd0686577adb8'
            '9917129ed8e6271733ff5ebd4149141216262f1df71e9e77bb3c8fc19b4676ab'
            'f409c43d8f4a2c8a486448bb75674f3b2bd1fdf58bb03a72a8427bcf5bda8e36'
            '9bc7c2d749bbcc576653c12efd8f513ebfb1415d4059b007ade783b39c0ffeca'
            )

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/win32-fix.patch
  patch -p1 -i ${srcdir}/python-m4.patch
  patch -p1 -i ${srcdir}/gio-types.patch
  WANT_AUTOMAKE=latest autoreconf -fi
}

build() {
  rm -rf python{2,3}-build
  mkdir python{2,3}-build devel
  CFLAGS+=" -Wno-error=format"

  for builddir in python{2,3}-build; do
    pushd ${builddir} > /dev/null
      PYTHON=${MINGW_PREFIX}/bin/${builddir%-build} \
      ../${_realname}-${pkgver}/configure \
        --prefix=${MINGW_PREFIX} \
        --build=${MINGW_CHOST} \
        --host=${MINGW_CHOST} \
        --target=${MINGW_CHOST} \
        --disable-silent-rules \
        --disable-introspection
      make VERBOSE=1
    popd > /dev/null
  done
}

check() {
  for builddir in python{2,3}-build; do
    pushd ${srcdir}/${builddir} > /dev/null
      PYTHON=${MINGW_PREFIX}/bin/${builddir%-build} \
      make check
    popd > /dev/null
  done
}

package_python2-gobject2() {
  pkgdesc="Python 2 bindings for GObject2 (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-glib2"
           "${MINGW_PACKAGE_PREFIX}-libffi"
           "${MINGW_PACKAGE_PREFIX}-gobject-introspection-runtime"
           "${MINGW_PACKAGE_PREFIX}-pygobject2-devel=${pkgver}")

  cd python2-build
  make DESTDIR="${pkgdir}" install

  find "${pkgdir}${MINGW_PREFIX}"/share/pygobject -name '*.py' -exec \
  sed -i '1s|#![ ]*/usr/bin/env python$|#!/usr/bin/env python2|' {} +

  rm -r "${pkgdir}${MINGW_PREFIX}"/share/gtk-doc

  # Split devel stuff
  cd ${srcdir}
  mkdir -p devel${MINGW_PREFIX}/{bin,lib,share/pygobject}
  mv "${pkgdir}${MINGW_PREFIX}"/bin/pygobject-codegen-2.0 devel${MINGW_PREFIX}/bin/
  mv {"${pkgdir}",devel}${MINGW_PREFIX}/include
  mv {"${pkgdir}",devel}${MINGW_PREFIX}/lib/pkgconfig
  mv {"${pkgdir}",devel}${MINGW_PREFIX}/share/pygobject/xsl
}

package_python3-gobject2() {
  pkgdesc="Python 3 bindings for GObject2 (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-glib2"
           "${MINGW_PACKAGE_PREFIX}-libffi"
           "${MINGW_PACKAGE_PREFIX}-gobject-introspection-runtime"
           "${MINGW_PACKAGE_PREFIX}-pygobject2-devel=${pkgver}")

  cd python3-build
  make DESTDIR="${pkgdir}" install

  # Delete devel stuff
  rm -f "${pkgdir}${MINGW_PREFIX}"/bin/pygobject-codegen-2.0
  rm -r "${pkgdir}${MINGW_PREFIX}"/{include,lib/pkgconfig,share/{gtk-doc,pygobject}}
}

package_pygobject2-devel() {
  pkgdesc="Development files for the pygobject bindings"

  mv ${srcdir}/devel${MINGW_PREFIX} "${pkgdir}/"
}

package_mingw-w64-i686-pygobject2-devel() {
  package_pygobject2-devel
}

package_mingw-w64-x86_64-pygobject2-devel() {
  package_pygobject2-devel
}

package_mingw-w64-i686-python2-gobject2() {
  package_python2-gobject2
}

package_mingw-w64-i686-python3-gobject2() {
  package_python3-gobject2
}

package_mingw-w64-x86_64-python2-gobject2() {
  package_python2-gobject2
}

package_mingw-w64-x86_64-python3-gobject2() {
  package_python3-gobject2
}
